import { legacy_createStore ,applyMiddleware  }from 'redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import { STORETYPE,LISTITEMTYPE }from '../Type/Store.d';
import {ALLITEMTYPE ,ALLLIST,SHOPLIST,LOVELIST,PREVBTN,NEXTBTN,INPBTN,ALLINPFLAG} from '../Type/SotreItem.d';

const initialState = {
    list : JSON.parse(window.localStorage.getItem('list') as any) || [],
    childrenList : JSON.parse(window.localStorage.getItem('childrenList') as any) || [],
    shopList : JSON.parse(window.localStorage.getItem('shopList') as any) || [],
    loveList : JSON.parse(window.localStorage.getItem('loveList') as any) || [],
    AllInpFlag : JSON.parse(window.localStorage.getItem('AllInpFlag') as any) || false,
    SumPrice : JSON.parse(window.localStorage.getItem('SumPrice') as any) || 0,

}

const routes = (state:STORETYPE = initialState, { type, payload }:ALLITEMTYPE) => {
    const Set_Item = (name:string,con:any)=>{
        window.localStorage.setItem(name,JSON.stringify(con));
    }
    const Sum_Price = ()=>{
        let sum = 0;
        state.shopList.forEach((item,index)=>{
            if(item.inpFlag) sum += item.price * item.count;
        })
        state.SumPrice = sum;
        Set_Item('SumPrice',state.SumPrice)
    }
    switch (type) {

    case ALLLIST:
        state.list = payload;
        state.childrenList = payload[0].children as LISTITEMTYPE[];
        Set_Item('list',state.list);
        Set_Item('childrenList',state.childrenList);
        return { ...state}

    case SHOPLIST:
        let flag = state.shopList.some(item=>item.id === payload.id);
        if(flag){
            state.shopList.forEach((item,index)=>{
                if(item.id === payload.id) item.count++
            })
        }else{
            payload.count = 1;
            payload.inpFlag = false;
            state.shopList.unshift(payload);
        }
        Set_Item('shopList',state.shopList);
        return { ...state}

    case LOVELIST:
        let childrenListCopy = [...state.childrenList];
        let loveflag = state.loveList.some(item=>item.id == payload.id);
        childrenListCopy.forEach((item,index)=>{
            if(item.id == payload.id) item.flag = !item.flag;
        })
        if(loveflag){
            state.loveList.forEach((item,index)=>{
                if(item.id == payload.id) state.loveList.splice(index,1)
            })
        }else{
            payload.flag = true;
            state.loveList.unshift(payload);
        }
        state.childrenList = childrenListCopy
        Set_Item('childrenList',state.childrenList);
        Set_Item('loveList',state.loveList);
        return { ...state}

    case PREVBTN:
        let shopprevCopy = [...state.shopList];
        shopprevCopy.forEach((item,index)=>{
            if(item.id === payload){
                if(item.count == 1) return;
                item.count--;
            }
        })
        state.shopList = shopprevCopy;
        Sum_Price();
        Set_Item('shopList',state.shopList);
        return { ...state}

    case NEXTBTN:
        let shopnextCopy = [...state.shopList];
        shopnextCopy.forEach((item,index)=>{
            if(item.id === payload){
                item.count++;
            }
        })
        state.shopList = shopnextCopy;
        Sum_Price();
        Set_Item('shopList',state.shopList);
        return { ...state}

    case INPBTN:
        let shopinpCopy = [...state.shopList];
        shopinpCopy.forEach((item,index)=>{
            if(item.id === payload){
                item.inpFlag = !item.inpFlag;
            }
        })
        state.AllInpFlag = shopinpCopy.every(item=>item.inpFlag);
        state.shopList = shopinpCopy;
        Sum_Price();
        Set_Item('shopList',state.shopList);
        Set_Item('AllInpFlag',state.AllInpFlag);
        return { ...state}

    case ALLINPFLAG:
        state.AllInpFlag = payload;
        let allinpshopCopy = [...state.shopList];
        allinpshopCopy.forEach((item,index)=>{
            item.inpFlag = payload;
        })
        state.shopList = allinpshopCopy;
        Sum_Price();
        Set_Item('shopList',state.shopList);
        Set_Item('AllInpFlag',state.AllInpFlag);
        return { ...state}

    // case INPBTN:
    //     return { ...state}

    // case INPBTN:
    //     return { ...state}

    default:
        return { ...state}
    }
}

export default legacy_createStore(routes,applyMiddleware(logger,thunk))
